import time
from threading import Thread
from app.commons.cronjobs import *

class Config(object):
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ENGINE_OPTIONS = {"pool_pre_ping": True,"pool_timeout":60}
    SQLALCHEMY_ECHO = False
    SQLALCHEMY_POOL_SIZE = 100
    SQLALCHEMY_MAX_OVERFLOW = 50
    SQLALCHEMY_POOL_RECYCLE = 1200

    # JOBS = [
    #     {
    #         "id": "test_job",
    #         "func": auto_update_price,
    #         "args": (),
    #         "trigger": "cron",
    #         "day":"*",
    #         "hour":"0/2",
    #         "minute":"0",
    #         "second":"0"
    #     }
    # ]
    #
    # SCHEDULER_API_ENABLED=True



def build_env_mysql(kwargs):
    return {
        "SQLALCHEMY_DATABASE_URI": f"mysql+pymysql://{kwargs['user']}:{kwargs['passwd']}@{kwargs['ip']}:{kwargs['port']}/{kwargs['db']}?charset=utf8mb4"
    }


def get_current_time(unit):
    if unit == 's':
        return int(time.time())
    elif unit == 'ms':
        return int(time.time() * 1000)


def async_exec(f):
    def wrapper(*args, **kwargs):
        thr = Thread(target=f, args=args, kwargs=kwargs)
        thr.start()

    return wrapper
